<?php

  /*

      TravianWAP
      Copyright (c) 2007-08 by Ananas - All rights reserved
      http://n00bz.biz/travianwap/
      Contact: uroskn@gmail.com

      --------------------------------------------------------------------------

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU  General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU  General Public License
      along with this program. If not, see http://www.gnu.org/licenses/.

  */

  echo "<strong>Rally point</strong><br/>";
  $sar = array(
    array("troops"  => "Troops"),
    array("send"    => "Send")
    //array("sim"     => "Simulator")
  );

  $tnames = array(
    array(),
    array("", "Legionnaire", "Praetorian", "Imperian", "Equites Legati", "Equites Imperatoris", "Equites Caesaris", "Battering Ram", "Fire Catapult", "Senator", "Settler", "Hero"),
    array("", "Phalanx", "Swordsman", "Pathfinder", "Theutates Thunder", "Druidrider", "Haeduan", "Ram", "Trebuchet", "Chieftain", "Settler", "Hero"),
    array("", "Clubswinger", "Spearman", "Axeman", "Scout", "Paladin", "Teutonic Knight", "Ram", "Catapult", "Chief", "Settler", "Hero"),
    array(""), // Fuck nature
    array("", "Pikeman", "Thorned Warrior", "Guardsman", "Birds Of Prey", "Axerider", "Natarian Knight", "Warelephant", "Ballista", "Natarian Emperor", "Settler", "Hero")
  );
  
  if (empty($req["submod"])) $submod = "troops";
  else $submod = $req["submod"];

  $first = "";
  foreach ($sar as $key => $value)
  {
    foreach ($value as $key => $val)
    {
      if ($key != $submod)
      {
        echo "$first";
        print_link("main.php?mod=details_build&amp;id={$req["id"]}&amp;submod=$key".ps(), $val);
        $first = "-";
      }
    }
  }
  echo "<br/><br/>";

  if ($submod == "send")
  {
    $ok = True;
    if (!empty($req["send"]))
    {
      $troops = array(
        process_number($req["t1"]), process_number($req["t2"]),
        process_number($req["t3"]), process_number($req["t4"]),
        process_number($req["t5"]), process_number($req["t6"]),
        process_number($req["t7"]), process_number($req["t8"]),
        process_number($req["t9"]), process_number($req["t10"]),
        process_number($req["t11"])
      );
      $xy = explode("/", $req["xy"]);
      $info = travian_send_troops($_SESSION, $troops, $req["attack_type"], CATAPULT_ANY, CATAPULT_ANY, SPY_RESOURCES_UNITS, $xy, $req["vname"], True);
      if (!empty($info["error"])) $error = $info["error"];
      else
      {
        $ok = False;
        switch ($info["c"])
        {
          case SEND_TROOPS_DEFENSE: $t = "Backup"; break;
          case SEND_TROOPS_ATTACK:  $t = "Full attack"; break;
          case SEND_TROOPS_RAID:    $t = "Raid"; break;
        }
        echo "[".implode(" ", $info["village"]["coord"])."]<br/>Player: {$info["player"]["name"]}<br/>Time: ".pdate($info["time"])."<br/>Arrival: ".pdate($info["at"]).
             "<br/>Village: {$info["village"]["name"]}<br/>Type: $t<br/>";
        $scout = False;
        if (($info["c"] == SEND_TROOPS_ATTACK) || ($info["c"] == SEND_TROOPS_RAID))
        {
          if (($req["rase"] == TROOPS_ROMANS) || ($req["rase"] == TROOPS_TEUTONS))
          {
            if ($troops[3] != 0) $scout = True;
          }
          else if ($troops[2] != 0) $scout = True;
        }
        if ($scout)
        {
          $pfields["scout_type"] = '$(scout_type)';
          $so = 'Scout options:<br/><select name="scout_type">
            <option value="1">Check Resources and troops</option>
            <option value="2">Check Defenses and troops</option>
          </select><br/>';
        }
        if (($info["c"] == SEND_TROOPS_ATTACK) && ($troops[7] != 0))
        {
          $pfields["cata1"] = '$(cata1)';
          $pfields["cata2"] = '$(cata2)';
          $catah = 'Cataupult 1 target:<br/><select name="cata1">
            <option value="99">Random</option>
            <option value="1">Woodcutter</option>
            <option value="2">Clay pit</option>
            <option value="3">Iron mine</option>
            <option value="4">Crop field</option>
            <option value="5">Sawmill</option>
            <option value="6">Brickyard</option>
            <option value="7">Iron foundry</option>
            <option value="8">Mill</option>
            <option value="9">Bakery</option>
            <option value="10">Warehouse</option>
            <option value="11">Granary</option>
            <option value="12">Bkacksmith</option>
            <option value="13">Aurmory</option>
            <option value="14">Tournament Square</option>
            <option value="15">Main building</option>
            <option value="16">Rally pint</option>
            <option value="17">Marketplace</option>
            <option value="18">Embassy</option>
            <option value="19">Barracks</option>
            <option value="20">Stable</option>
            <option value="21">Workshop</option>
            <option value="22">Academy</option>
            <option value="24">Town hall</option>
            <option value="25">Residence</option>
            <option value="26">Palace</option>
            <option value="27">Tresoury</option>
            <option value="28">Trade office</option>
            <option value="29">Great barracs</option>
            <option value="30">Great stable</option>
            <option value="37">Heroe\'s residence</option>
          </select><br/>
          Cataupult 2 target:<br/><select name="cata2">
            <option value="99">Random</option>
            <option value="1">Woodcutter</option>
            <option value="2">Clay pit</option>
            <option value="3">Iron mine</option>
            <option value="4">Crop field</option>
            <option value="5">Sawmill</option>
            <option value="6">Brickyard</option>
            <option value="7">Iron foundry</option>
            <option value="8">Mill</option>
            <option value="9">Bakery</option>
            <option value="10">Warehouse</option>
            <option value="11">Granary</option>
            <option value="12">Bkacksmith</option>
            <option value="13">Aurmory</option>
            <option value="14">Tournament Square</option>
            <option value="15">Main building</option>
            <option value="16">Rally pint</option>
            <option value="17">Marketplace</option>
            <option value="18">Embassy</option>
            <option value="19">Barracks</option>
            <option value="20">Stable</option>
            <option value="21">Workshop</option>
            <option value="22">Academy</option>
            <option value="24">Town hall</option>
            <option value="25">Residence</option>
            <option value="26">Palace</option>
            <option value="27">Tresoury</option>
            <option value="28">Trade office</option>
            <option value="29">Great barracs</option>
            <option value="30">Great stable</option>
            <option value="37">Heroe\'s residence</option>
            <option value="40">World wonder</option>
          </select><br/>';
        }
        else
        {
          $cval1 = '$(cata1)';
          $cval2 = '$(cata2)';
        }
        //echo "<anchor>Send troops<go href=\"main.php".ps('?')."\" method=\"get\">";
        foreach ($info as $key => $value)
        {
          if ($key == "coord") $value = implode("/", $value);
          if (!is_array($value)) $pfields[$key] = $value;
        }
        $pfields["submod"] = "send";
        $pfields["ssend"]  = "ssend";
        $pfields["mod"]    = "details_build";
        $pfields["stfu"]   = 1;
        $pfields["coord"]  = implode("/", $info["village"]["coord"]);
        print_form_top("main.php".ps('?'), $pfields, "post");
        echo $so.$catah;
        print_forM_bottom("Send troops");
      }
    }
    if (!empty($req["ssend"]))
    {
      $troops = array($req["t1"], $req["t2"], $req["t3"], $req["t4"], $req["t5"],
                      $req["t6"], $req["t7"], $req["t8"], $req["t9"], $req["t10"],
                      $req["t11"]);
      $xy = explode("/", $req["coord"]);
      //travian_send_troops($_SESSION, $troops, $req["attack_type"], CATAPULT_ANY, CATAPULT_ANY, SPY_RESOURCES_UNITS, $xy, $req["vname"], True);
      travian_send_troops($_SESSION, $troops, $req["c"], $req["cata1"], $req["cata2"], $req["scout_type"], $xy);
      $error = "Troops send.";
    }
    if ($ok)
    {
      $troops = travian_return_srtc($_SESSION);
      if (!empty($error)) echo "$error<br/><br/>";
      $tstr = NULL;
      for ($x = 1; $x <= 10; $x++)
      {
        $tstr .= $tnames[$troops["type"]][1]." [{$troops[1]}]".':<br/><input name="t1" type="text" value="0"/><br/>';
        $pfield["t$x"] = '$(t'.$x.')';
      }
      $pfield["hero"]        = '$(hero)';
      $pfield["xy"]          = '$(xy)';
      $pfield["vname"]       = '$(vname)';
      $pfield["attack_type"] = '$(attack_type)';
      $pfield["rase"]        = $troops["type"];
      $pfield["stfu"]        = 1;
      $pfield["send"]        = "send";
      $pfield["id"]          = $req["id"];
      $pfield["submod"]      = "send";
      $pfield["mod"]         = "details_build";

      print_form_top("main.php".ps('?'), $pfield, "post");

      ?><?php echo $tnames[$troops["type"]][1]." [{$troops[1]}]";?>:<br/><input name="t1" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][2]." [{$troops[2]}]";?>:<br/><input name="t2" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][3]." [{$troops[3]}]";?>:<br/><input name="t3" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][4]." [{$troops[4]}]";?>:<br/><input name="t4" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][5]." [{$troops[5]}]";?>:<br/><input name="t5" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][6]." [{$troops[6]}]";?>:<br/><input name="t6" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][7]." [{$troops[7]}]";?>:<br/><input name="t7" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][8]." [{$troops[8]}]";?>:<br/><input name="t8" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][9]." [{$troops[9]}]";?>:<br/><input name="t9" type="text" value="0"/><br/>
        <?php echo $tnames[$troops["type"]][10]." [{$troops[10]}]";?><br/><input name="t10" type="text" value="0"/><br/>
        Hero:<br/><input name="t11" type="text" value="0"/><br/>
        X/Y coordinates:<br/><input name="xy" type="text" value="0/0"/><br/>
        Village name:<br/><input name="vname" type="text" value=""/><br/>
        Attack type:<br/>
        <select name="attack_type">
          <option value="2">Backup</option>
          <option value="3">Full attack</option>
          <option value="4">Raid attack</option>
        </select><br/>
      <?php
        /*<anchor>Send troops<go href="main.php<?php echo ps('?')?>" method="get"><postfield name="mod" value="details_build"/>
        <postfield name="id" value="<?php echo $req["id"]; ?>"/><postfield name="submod" value="send"/>
        <postfield name="t1" value="$(t1)"/><postfield name="t2" value="$(t2)"/>
        <postfield name="t3" value="$(t3)"/><postfield name="t4" value="$(t4)"/>
        <postfield name="t5" value="$(t5)"/><postfield name="t6" value="$(t6)"/>
        <postfield name="t7" value="$(t7)"/><postfield name="t8" value="$(t8)"/>
        <postfield name="t9" value="$(t9)"/><postfield name="t10" value="$(t10)"/><postfield name="t11" value="$(t11)"/>
        <postfield name="xy" value="$(xy)"/><postfield name="vname" value="$(vname)"/>
        <postfield name="attack_type" value="$(attack_type)"/><postfield name="send" value="send"/>
        <postfield name="rase" value="<?php echo $troops["type"];?>"/>
        <postfield name="stfu" value="1"/></go></anchor><br/>
        <?php         */
      print_form_bottom("Send");
    }
  }
  if ($submod == "rtroops")
  {
    if (empty($req["frec"]))
    {
      $info = travian_recall_troops($_SESSION, $req["d"], $req["c"], NULL, True);
      for ($i = 0; $i <= 10; $i++)
      {
        if (!empty($info["troops"][$i]))
        {
          $pfields['t'.($i + 1)] = '$(t'.($i + 1).')';
          $ida = $tnames[$info["type"]][$i + 1]." [".$info["troops"][$i]."]:<br/>".
                 '<input type="text" name="t'.($i + 1).'" value="'.$info["troops"][$i].'"/><br/>';
        }
      }
      $pfields["mod"]    = "details_build";
      $pfields["id"]     = $req["id"];
      $pfields["submod"] = "rtroops";
      $pfields["frec"]   = 1;
      $pfields["c"]      = $req["c"];
      $pfields["d"]      = $req["d"];
      print_form_top("main.php".ps('?'), $pfields, "get");
      echo $ida;
      print_form_bottom("Recall troops");
      echo "<br/>";
    }
    else
    {
      $submod = "";
      for ($i = 0; $i <= 10; $i++)
      {
        if (!empty($req["t".($i + 1)])) $troops[$i] = $req["t".($i + 1)];
        else $troops[$i] = 0;
      }
      travian_recall_troops($_SESSION, $req["d"], $req["c"], $troops);
      echo "Troops recalled<br/><br/>";
    }
  }
  if ((empty($submod)) || ($submod == "troops"))
  {
    //error_reporting(E_ALL);
    $info = travian_return_rally_point_info($_SESSION);
    //print_r($info);
    for ($i = 0; $i < count($info); $i++)
    {
      echo "<b>{$info[$i]["title"]["title"]}</b><br/>Originates: {$info[$i]["orign"]["name"]}<br/>Rase: ";
      if ($info[$i]["type"] == TROOPS_ROMANS) echo "Romans";
      else if ($info[$i]["type"] == TROOPS_GAULS) echo "Gauls";
      else if ($info[$i]["type"] == TROOPS_TEUTONS) echo "Teutons";
      else echo "Natars";
      echo "<br/>";
      for ($x = 0; $x <= 11; $x++)
      {
        if ((!empty($info[$i]["troops"][$x])) && ($info[$i]["troops"][$x] != "?")) echo $tnames[$info[$i]["type"]][$x + 1].": ".$info[$i]["troops"][$x]."<br/>";
      }
      if (!empty($info[$i]["consumes"])) echo $info[$i]["consumes"]." crop/hour<br/>";
      if (!empty($info[$i]["arrival"]))
      {
        echo pdate($info[$i]["arrival"])." - ".pdate($info[$i]["at"])."<br/>";
        if (!empty($info[$i]["callbackurl"])) print_link("main.php?mod=direct_call&amp;".
                                                          htmlspecialchars($info[$i]["callbackurl"]).
                                                          "&amp;return=details_build&amp;submod=&amp;".
                                                          "file=build".ps(), "Abort", "<br/>");
      }
      elseif (!empty($info[$i]["callbackurl"])) print_link("main.php?mod=details_build&amp;id={$req["id"]}".
                                                           "&amp;".htmlspecialchars($info[$i]["callbackurl"])."&amp;submod=rtroops".
                                                           ps(), "Call back", "<br/>");
      if (!empty($info[$i]["shoot_hostage"])) print_link("main.php?mod=direct_call&amp;".
                                                         htmlspecialchars($info[$i]["shoot_hostage"]).
                                                         "&amp;return=details_build&amp;submod=&amp;".
                                                         "file=build".ps(), "Shoot hostage", "<br/>");
      echo "<br/>";
    }
  }


?>
